﻿/// <reference path="../../knockout-2.1.0.js" />
/// <reference path="../../knockout.mapping-latest.js" />
/// <reference path="../shared/utils.js" />
/// <reference path="../../jquery-1.7.2-vsdoc.js" />
/// <reference path="../../knockout.wrap.js" />
var mapping = {
    Peso: {
        create: function (info) {
            return ko.observable(info.data).extend({ numeric: false });
        }
    },
    Talla: {
        create: function (info) {
            return ko.observable(info.data).extend({ numeric: false });
        }
    },
    Temperatura: {
        create: function (info) {
            return ko.observable(info.data).extend({ numeric: false });
        }
    }
};

function Historia(data) {
    var self = this;
    self.FechaIngreso = ko.observable();
    self.Load = function (info) {
        ko.mapping.fromJS(info, {}, this);
    };

    if (data)
        self.Load(data);
}

function HistoriaVM(model) {
    var self = this;
    self.Historia = ko.observable();
    self.Mensaje = ko.observable();
    self.HaveErrors = ko.observable();
    self.Load = function (response) {
        self.Historia(new Historia(response));
        if (model.HistoriaClinicaPacienteId != null) {
            individuoDetalleControl.Load(model.HistoriaClinicaPacienteId);
            historiaClinicaRegistroControl.Load(model.HistoriaClinicaPacienteId);
        }
    };

    self.LoadIncidente = function (id) {
        $.ajax({
            url: '/Historia/ObtenerIncidenteHistoriaClinica',
            type: "GET",
            data: { registroId: id },
            async: false,
            success: function (data) {
                self.Historia(new Historia(data.schema));
            }
        });
    };

    self.Guardar = function () {
        ajax('/historia/Guardar', ko.mapping.toJSON(self.Historia()), function (result) {
            if (result.isValid) {
                self.Mensaje("La historial para el usuario </b>");
                $('#modalsaved').modal(); 
                /*algo*/
            } else {
                //self.HaveErrors(true);
                self.Mensaje(result.message);
                $('#modalError').modal();
            }
        });
    };
    if (model)
        self.Load(model);
};

var historiaIncidenteViewModel = new HistoriaVM();

function startApp(model) {
    historiaIncidenteViewModel = new HistoriaVM(model);
    ko.applyBindings(historiaIncidenteViewModel, document.getElementById("historiaclinicaContext"));
}
